#!/bin/bash
{
	#////////////////////////////////////
	# DietPi CPU Info Script
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Prints CPU information
	#
	# Usage:
	# - /boot/dietpi/dietpi-cpuinfo 	Obtain stats and print
	# - /boot/dietpi/dietpi-cpuinfo 2 	Generates file of available, ordered scaling freq's $FP_CPU_SCALINGAVAILABLE_FREQ
	#////////////////////////////////////

	# Import DietPi-Globals --------------------------------------------------------------
	. /boot/dietpi/func/dietpi-globals
	G_PROGRAM_NAME='DietPi-CPU_info'
	G_CHECK_ROOT_USER
	G_INIT
	# Import DietPi-Globals --------------------------------------------------------------

	# Grab input
	disable_error=1 G_CHECK_VALIDINT "$1" && INPUT=$1 || INPUT=0

	aCPU_CURRENT_FREQ=()
	aCPU_MIN_FREQ=()
	aCPU_MAX_FREQ=()
	Obtain_Cpu_Freq(){

		for ((i=0; i<$G_HW_CPU_CORES; i++))
		do

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq ]]; then

				aCPU_CURRENT_FREQ[$i]=$(<"/sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq")

			fi

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq ]]; then

				aCPU_MIN_FREQ[$i]=$(<"/sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq")

			fi

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq ]]; then

				aCPU_MAX_FREQ[$i]=$(<"/sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq")

			fi

		done

	}

	aCPU_SCALINGAVAILABLE_FREQ=()
	CPU_SCALINGSUPPORTED=1
	readonly FP_CPU_SCALINGAVAILABLE_FREQ='/tmp/dietpi-available_cpu_freqs'
	Obtain_Cpu_Scaling_Freq(){

		# Intel
		if [[ -f '/sys/devices/system/cpu/intel_pstate/max_perf_pct' ]]; then

			local i
			for ((i=10; i<=100; i+=5))
			do

				aCPU_SCALINGAVAILABLE_FREQ+=("$i")

			done

		# Standard
		elif [[ -f '/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies' ]]; then

			for ((i=0; i<$G_HW_CPU_CORES; i++))
			do

				aCPU_SCALINGAVAILABLE_FREQ[$i]=$(tr ' ' '\n' < "/sys/devices/system/cpu/cpu$i/cpufreq/scaling_available_frequencies" | sed 's/[^0-9]//g' | sed '/^$/d')

			done

		# XU4 3.x
		elif [[ -f '/sys/devices/system/cpu/cpufreq/mp-cpufreq/cpu_freq_table' ]]; then

			aCPU_SCALINGAVAILABLE_FREQ[0]=$(tr ' ' '\n' < /sys/devices/system/cpu/cpufreq/mp-cpufreq/cpu_freq_table | sed 's/[^0-9]//g' | sed '/^$/d')

		else

			CPU_SCALINGSUPPORTED=0

		fi

	}

	CPU_TEMP_PRINT=
	Obtain_Cpu_Temp(){ CPU_TEMP_PRINT=$(print_full_info=1 G_OBTAIN_CPU_TEMP); }

	CPU_GOV_CURRENT='N/A'
	Obtain_Cpu_Gov(){

		[[ -f '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' ]] && CPU_GOV_CURRENT=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

	}

	Print_Stats(){

		echo -e "\n \e[38;5;154m─────────────────────────────────────────────────────\e[0m\n \e[1mDietPi CPU Info\n\e[90m Use dietpi-config to change CPU / performance options\e[0m\n \e[38;5;154m─────────────────────────────────────────────────────\e[0m"

		# VM does not support most info
		(( $G_HW_MODEL == 20 )) && echo -e '\n\e[90m[\e[93mWARNING\e[90m] \e[97mMost CPU information is not available on VM.\e[0m\n'

		# Processor Arch
		echo -e " Architecture \e[90m|\e[0m     $(uname -m)"

		# Current Temp
		echo -e " Temperature  \e[90m|\e[0m     $CPU_TEMP_PRINT"

		# Current Gov
		echo -e " Governor     \e[90m|\e[0m     $CPU_GOV_CURRENT"

		if [[ $CPU_GOV_CURRENT == 'ondemand' || $CPU_GOV_CURRENT == 'conservative' ]]; then

			echo -e " Throttle up  \e[90m|\e[0m     $(sed -n '/^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)% CPU usage"

		fi

		# Current Frequency
		(( ${#aCPU_CURRENT_FREQ[@]} )) && echo -e '\n \e[90m                Current Freq    Min Freq   Max Freq\e[0m'
		for i in "${!aCPU_CURRENT_FREQ[@]}"
		do

			echo -e " \e[90mCPU$i         |\e[0m      $(( ${aCPU_CURRENT_FREQ[$i]:-0} / 1000 )) MHz      \e[90m$(( ${aCPU_MIN_FREQ[$i]:-0} / 1000 )) MHz    $(( ${aCPU_MAX_FREQ[$i]:-0} / 1000 )) MHz\e[0m"

		done

		echo

		# CPU scaling may bump up with script
		G_DIETPI-NOTIFY 2 'CPU current frequency, may be affected by this script, due to the processing required to run it.\n'

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	# Return mode
	if (( $INPUT == 2 )); then

		Obtain_Cpu_Scaling_Freq

		# Export to file
		> $FP_CPU_SCALINGAVAILABLE_FREQ

		if (( $CPU_SCALINGSUPPORTED )); then

			for i in "${aCPU_SCALINGAVAILABLE_FREQ[@]}"
			do

				echo "$i" >> $FP_CPU_SCALINGAVAILABLE_FREQ

			done

			# Order, remove dupes
			sort -u -n $FP_CPU_SCALINGAVAILABLE_FREQ -o $FP_CPU_SCALINGAVAILABLE_FREQ

		fi

	# Print all stats
	else

		Obtain_Cpu_Freq
		Obtain_Cpu_Temp
		Obtain_Cpu_Gov

		Print_Stats

	fi

	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}
